package sg.com.techstudio.ecc.validator;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.stereotype.Component;

import sg.com.techstudio.ecc.dao.RegisterDao;
import sg.com.techstudio.ecc.dao.SystemConfigDao;
import sg.com.techstudio.ecc.obj.ParticipantObj;
import sg.com.techstudio.ecc.obj.RegisterObj;

@Component
public class RegisterObjValidator extends BaseValidator {
	private static final Log log = LogFactory.getLog(RegisterObjValidator.class);
	
	@Autowired
	private SystemConfigDao systemConfigDao;
	
	@Autowired
	private RegisterDao registerDao;
	
	@Autowired
	private ParticipantObjValidator participantObjValidator;
		
	public void validateRegisterEntry(RegisterObj r, ValidationContext context) {
		log.debug("validateRegisterEntry() entry");
		MessageContext messages = context.getMessageContext();
		
		validateEmptyString(r.getSalutation(), "salutation", messages);
		validateEmptyString(r.getName(), "name", messages);
		validateEmptyString(r.getDesignation(), "designation", messages);
		validateEmptyString(r.getCompany(), "company", messages);
		validateEmail(r.getEmail(), "email", messages);
		validateEmptyString(r.getAddress(), "address", messages);
		if(validateEmptyString(r.getPostalCode(), "postalCode", messages))
			if(!POSTCODE_PATTERN.matcher(r.getPostalCode()).matches())
				addMessage("postalCode", "invalid.postalCode", messages);
//		validateEmptyString(r.getCountry(), "country", messages);
//		if(StringUtils.isBlank(r.getTelCc())||StringUtils.isBlank(r.getTelNo()))
//			addMessage("telNo", "required.field", messages);
//		validateEmail(r.getContactEmail(), "contactEmail", false, messages);
//		if(!StringUtils.isBlank(r.getPromoCode()) && !registerDao.isTodayBeforeEarlyBird())
//			if(!systemConfigDao.validatePromoCode(r.getPromoCode()))
//				addMessage("promoCode", "invalid.couponCode", messages);
//		if(validateEmptyString(r.getTelephone(), "telephone", messages))
//			if(!PHONE_PATTERN.matcher(r.getTelephone()).matches())
//				addMessage("telephone", "invalid.telephone", messages);
		if(validateEmptyString(r.getMobile(), "mobile", messages))
			if(!MOBILE_PATTERN.matcher(r.getMobile()).matches())
				addMessage("mobile", "invalid.telephone", messages);
		if(!StringUtils.isBlank(r.getOfficeNo())
				&& !PHONE_PATTERN.matcher(r.getOfficeNo()).matches())
			addMessage("officeNo", "invalid.telephone", messages);
		if(!NRIC_PATTERN.matcher(r.getNric()).matches() || !validateSGNric(r.getNric()))
			addMessage("nric", "invalid.nric", messages);
		else if(validateNricDuplicate(r.getNric(), r.getParticipantList(), r.getRegType()))
			addMessage("nric", "nric.duplicate", messages);
		if(r.getRegType()==0) {
			validateEmptyString(r.getBadgeName(), "badgeName", messages);
			if(r.getSessionA()==null)
				addMessage("sessionA", "required.session", messages);
			if(r.getSessionB()==null)
				addMessage("sessionB", "required.session", messages);
			if(r.getSessionC()==null)
				addMessage("sessionC", "required.session", messages);
			if(r.getSessionD()==null)
				addMessage("sessionD", "required.session", messages);
			if(r.getVegetarian()==null)
				addMessage("vegetarian", "required.selectfield", messages);
		} else if(r.getRegType()==1) {
			if(r.getLearningJourney()==null)
				addMessage("learningJourney", "required.learningjourney", messages);
		}
	}
	
	protected boolean validateNricDuplicate(String nric, List<ParticipantObj> participantList, int regType) {
		if(registerDao.isNricExist(nric, regType))
			return true;
		if(participantList==null || participantList.isEmpty())
			return false;
		for(ParticipantObj p: participantList)
			if(StringUtils.equalsIgnoreCase(nric, p.getNric()))
				return true;
		return false;
	}
	
	public void validateRegisterParticipant(RegisterObj r, ValidationContext context) {
		log.debug("validateRegisterParticipant() entry");
		
		if(r==null|| r.getParticipantCount()==0)
			return;
		participantObjValidator.validateParticipantList(r, context);
	}
}
